<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务器url" prop="serverUrl">
        <el-input
          v-model="queryParams.serverUrl"
          placeholder="请输入服务器url"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授权应用id" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入授权应用id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授权应用秘钥" prop="clientSecret">
        <el-input
          v-model="queryParams.clientSecret"
          placeholder="请输入授权应用秘钥"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回调地址" prop="callbackUrl">
        <el-input
          v-model="queryParams.callbackUrl"
          placeholder="请输入回调地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授权码路径" prop="authorizePath">
        <el-input
          v-model="queryParams.authorizePath"
          placeholder="请输入授权码路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="accessToken路径" prop="tokenPath">
        <el-input
          v-model="queryParams.tokenPath"
          placeholder="请输入accessToken路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户信息url" prop="userInfoUrl">
        <el-input
          v-model="queryParams.userInfoUrl"
          placeholder="请输入用户信息url"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="openId路径" prop="openIdPath">
        <el-input
          v-model="queryParams.openIdPath"
          placeholder="请输入openId路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="openId" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入openId"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['social:social_platform:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['social:social_platform:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['social:social_platform:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['social:social_platform:export']"
        >导出</el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="social_platformList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="平台名" align="center" prop="platformName" />
      <el-table-column label="服务器url" align="center" prop="serverUrl" />
      <el-table-column label="授权应用id" align="center" prop="clientId" />
      <el-table-column label="授权应用秘钥" align="center" prop="clientSecret" />
      <el-table-column label="回调地址" align="center" prop="callbackUrl" />
      <el-table-column label="授权码路径" align="center" prop="authorizePath" />
      <el-table-column label="accessToken路径" align="center" prop="tokenPath" />
      <el-table-column label="用户信息url" align="center" prop="userInfoUrl" />
      <el-table-column label="openId路径" align="center" prop="openIdPath" />
      <el-table-column label="openId" align="center" prop="openId" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['social:social_platform:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['social:social_platform:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改社交应用对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务器url" prop="serverUrl">
          <el-input v-model="form.serverUrl" placeholder="请输入服务器url" />
        </el-form-item>
        <el-form-item label="授权应用id" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入授权应用id" />
        </el-form-item>
        <el-form-item label="授权应用秘钥" prop="clientSecret">
          <el-input v-model="form.clientSecret" placeholder="请输入授权应用秘钥" />
        </el-form-item>
        <el-form-item label="回调地址" prop="callbackUrl">
          <el-input v-model="form.callbackUrl" placeholder="请输入回调地址" />
        </el-form-item>
        <el-form-item label="授权码路径" prop="authorizePath">
          <el-input v-model="form.authorizePath" placeholder="请输入授权码路径" />
        </el-form-item>
        <el-form-item label="accessToken路径" prop="tokenPath">
          <el-input v-model="form.tokenPath" placeholder="请输入accessToken路径" />
        </el-form-item>
        <el-form-item label="用户信息url" prop="userInfoUrl">
          <el-input v-model="form.userInfoUrl" placeholder="请输入用户信息url" />
        </el-form-item>
        <el-form-item label="openId路径" prop="openIdPath">
          <el-input v-model="form.openIdPath" placeholder="请输入openId路径" />
        </el-form-item>
        <el-form-item label="openId" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入openId" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSocial_platform, getSocial_platform, delSocial_platform, addSocial_platform, updateSocial_platform, exportSocial_platform } from "@/api/social/social_platform";

export default {
  name: "Social_platform",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社交应用表格数据
      social_platformList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serverUrl: null,
        clientId: null,
        clientSecret: null,
        callbackUrl: null,
        authorizePath: null,
        tokenPath: null,
        userInfoUrl: null,
        openIdPath: null,
        openId: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询社交应用列表 */
    getList() {
      this.loading = true;
      listSocial_platform(this.queryParams).then(response => {
        this.social_platformList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        platformName: null,
        serverUrl: null,
        clientId: null,
        clientSecret: null,
        callbackUrl: null,
        authorizePath: null,
        tokenPath: null,
        userInfoUrl: null,
        openIdPath: null,
        openId: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.platformName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加社交应用";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const platformName = row.platformName || this.ids
      getSocial_platform(platformName).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改社交应用";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.platformName != null) {
            updateSocial_platform(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSocial_platform(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const platformNames = row.platformName || this.ids;
      this.$confirm('是否确认删除社交应用编号为"' + platformNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSocial_platform(platformNames);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有社交应用数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSocial_platform(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
